package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class OrdersController {
    private OrdersService service = new OrdersServiceImpl();

    public Object createOrders(HttpServletRequest request) throws Exception{
        Orders orders = new Orders();
        int oId;
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Double totalPrice = Double.valueOf(request.getParameter("totalPrice"));
        Integer orderState = Integer.valueOf(request.getParameter("orderState"));
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setOrderTotal(totalPrice);
        orders.setOrderState(orderState);
        orders.setDaId(daId);
        oId= service.createOrders(orders);
        return oId;
    }

    public Object getOrdersById(HttpServletRequest request) throws Exception{
        Orders orders;
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        orders= service.getOrdersById(orderId);
        return orders;
    }

    public Object listOrdersByUserId(HttpServletRequest request) throws Exception{
        List<Orders> list;
        String userId = request.getParameter("userId");
        list= service.listOrdersByUserId(userId);
        return list;
    }


    public Object updateOrders(HttpServletRequest request) throws Exception{
        int row;
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        Integer orderState = Integer.valueOf(request.getParameter("orderState"));
        row = service.updateOrders(orderId,orderState);
        return row;
    }
}
